package arithmetic2.day04;

import java.util.Arrays;
import java.util.List;

/*
    打印两个链表的公共部分
 */
public class Demo02 {
    public static void main(String[] args) {
        List<Integer> list1 = Arrays.asList(1,3,7,4,2,4,7);
        List<Integer> list2 =  Arrays.asList(2,3,7,4,2,1);
        common(list1,list2);
    }

    //打印两个链表的公共部分
    public static void common(List<Integer> list1,List<Integer> list2){
        int index1 = 0; //list1的指针
        int index2 = 0; //list2的指针
        while (index1 < list1.size() && index2 <list2.size()){
            if(list1.get(index1)<list2.get(index2)){
                index1++;
            }else if(list1.get(index1)==list2.get(index2)){
                System.out.print(list1.get(index1)+" ");
                index1++;
                index2++;
            }else {
                index2++;
            }
        }
    }
}
